/**
 * @file app\tasks\mobus\devices\_mobus_command.c
 *
 * Copyright (C) 2023
 *
 * _mobus_command.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author HinsShum hinsshum@qq.com
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "protocol_mobus.h"
#include "storage.h"

/*---------- macro ----------*/
#define relay_command_off 1
#define relay_command_on 2
#define relay_command_value 3
#define relay_command_negation 0x55
#define relay_command_negation 0x55

#define led_on 1
#define led_off 2
#define led_value 3
#define key_enable_command 4
#define key_disenable_command 5
#define key_all_disenable_command 7
#define key_all_enable_command 8
#define key_ledx_control 0x10
#define Lisicar_dev_mode 0x12//丽思卡尔顿定制面板模式

/*---------- type define ----------*/
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
/*---------- variable ----------*/
static struct storage_led_struct* led = NULL;
uint8_t dev_channlx_sta_buffer[8] = { 0 };
/*---------- function ----------*/
static void _mobus_command(mobus_device_t self, uint8_t sub_channel,
    uint8_t* param, uint8_t length) {
    bool dly_flag = false;
    memset(dev_channlx_sta_buffer, 0, sizeof(dev_channlx_sta_buffer));
    assert(self);
    assert(param);
    assert(sub_channel < self->channel_nums);
    uint8_t command = param[0];
    if (sub_channel == 0) {
        char key_num = 0;
        unsigned char led_value_t = 0;
        led = (struct storage_led_struct*)storage_get(STORAGE_AREA_LIGHT_MODE);
        if (command == key_ledx_control) {
            if (no_storage.LisicarD_Temp.net_mode != 1)
            {
                xlog_error("net_mode is 0 please open net_mode");
                return;
            }
            led->LisicarD_struct.background = (uint16_t)(param[1] | (param[2] << 8));

            dly_flag = true;
            no_storage.LisicarD_Temp.s1_led_sat = param[1] & 0x01;
            no_storage.LisicarD_Temp.s2_led_sat = (param[1] >> 1) & 0x01;
            no_storage.LisicarD_Temp.s3_led_sat = (param[1] >> 2) & 0x01;
            no_storage.LisicarD_Temp.s4_led_sat = (param[1] >> 3) & 0x01;
            no_storage.LisicarD_Temp.s5_led_sat = (param[1] >> 4) & 0x01;
            no_storage.LisicarD_Temp.s6_led_sat = (param[1] >> 5) & 0x01;
            no_storage.LisicarD_Temp.s7_led_sat = (param[1] >> 6) & 0x01;
            no_storage.LisicarD_Temp.s8_led_sat = (param[1] >> 7) & 0x01;

            no_storage.LisicarD_Temp.backgroud_led = param[2] & 0x01;
            no_storage.LisicarD_Temp.null_1 = (param[2] >> 1) & 0x01;
            no_storage.LisicarD_Temp.null_2 = (param[2] >> 2) & 0x01;
            no_storage.LisicarD_Temp.null_3 = (param[2] >> 3) & 0x01;
            no_storage.LisicarD_Temp.s9_led_sat = (param[2] >> 4) & 0x01;
            no_storage.LisicarD_Temp.s10_led_sat = (param[2] >> 5) & 0x01;
            no_storage.LisicarD_Temp.s11_led_sat = (param[2] >> 6) & 0x01;
            no_storage.LisicarD_Temp.s12_led_sat = (param[2] >> 7) & 0x01;
        }
        else if (command == Lisicar_dev_mode) {
            led->LisicarD_struct.LisicarD_sys = param[1];
            dly_flag = true;
            no_storage.LisicarD_Temp.autoplay_mode = param[1] & 0x01;
            no_storage.LisicarD_Temp.net_mode = (param[1] >> 1) & 0x01;
            no_storage.LisicarD_Temp.key_voluntary_mode = (param[1] >> 2) & 0x01;
            no_storage.LisicarD_Temp.pir_enable_flag = (param[1] >> 3) & 0x01;
            no_storage.LisicarD_Temp.ledx_dlydie_flag = (param[1] >> 4) & 0x01;
            no_storage.LisicarD_Temp.press_up_send_enable = (param[1] >> 5) & 0x01;
        }
        if (dly_flag == true) {
            storage_set(STORAGE_AREA_LIGHT_MODE, led, sizeof(struct storage_led_struct));
            dev_channlx_sta_buffer[0] = 0;
            dev_channlx_sta_buffer[1] = 0;
            dev_channlx_sta_buffer[2] = 0;
            dev_channlx_sta_buffer[3] = (led->LisicarD_struct.background >> 8) & 0xFF;//小端;
            dev_channlx_sta_buffer[4] = led->LisicarD_struct.background & 0xFF;
            dev_channlx_sta_buffer[5] = 0;
            dev_channlx_sta_buffer[6] = 0;
            dev_channlx_sta_buffer[7] = led->LisicarD_struct.LisicarD_sys;
            protocol_mobus_dev_report(MOBUS_FRAME_TYPE_UPLOAD_STATUS, sub_channel, dev_channlx_sta_buffer, 8);
            xlog_info("background led = 0x%x , LisicarD_sys = 0x%x \n", led->LisicarD_struct.background, led->LisicarD_struct.LisicarD_sys);
            no_storage.led_temp_variable.dev_work_mode = LED_MODE_UPDATA;
        }
    }
    else if (sub_channel >= 1) {
        char relay_id = 0;
        static char negation_flag = 0;
        relay_id = sub_channel - 1;
        if (length == 1) {

        }
    }
}
static void _report_channlx_information(mobus_device_t self, uint8_t sub_channel) {
    memset(dev_channlx_sta_buffer, 0, sizeof(dev_channlx_sta_buffer));
    if (sub_channel == 0) {
        char key_num = 0;
        led = (struct storage_led_struct*)storage_get(STORAGE_AREA_LIGHT_MODE);
        dev_channlx_sta_buffer[0] = 0;
        dev_channlx_sta_buffer[1] = 0;
        dev_channlx_sta_buffer[2] = 0;
        dev_channlx_sta_buffer[3] = (led->LisicarD_struct.background >> 8) & 0xFF;//小端;
        dev_channlx_sta_buffer[4] = led->LisicarD_struct.background & 0xFF;
        dev_channlx_sta_buffer[5] = 0;
        dev_channlx_sta_buffer[6] = 0;
        dev_channlx_sta_buffer[7] = led->LisicarD_struct.LisicarD_sys;
        protocol_mobus_dev_report(MOBUS_FRAME_TYPE_UPLOAD_STATUS, sub_channel, dev_channlx_sta_buffer, 8);
    }
    else if (sub_channel >= 1) {
    }

}